﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeSmith.SchemaHelper.Util
{
    public static class ADONameHelper
    {
        private static Dictionary<string, string> cache;
        const string ConnectionName = "ConnectionName";
        const string CommandName = "CommandName";
        const string ParameterName = "ParameterName";
        const string ClientNameSpaceName = "NameSpaceName";
        const string DbTypeNameSpaceName = "DbTypeNameSpaceName";
        const string NamePrefixName = "NamePrefixName";
        const string TimestampName = "TimestampName";
        const string AssemblyName = "AssemblyName";
        static ADONameHelper()
        {
            cache = new Dictionary<string, string>();
            Reset();    
        }

        public static string FormatADOParameter(string paraString)
        {
            string result = paraString;
            switch (Configuration.Instance.DataSourceType)
            {                    
                case DataSourceTypeEnum.Oracle:
                    if (!paraString.StartsWith(":"))
                    {
                        result = ":" + paraString;
                    }
                    break;
            }
            return result;
        }

        public static void Reset()
        {
            cache.Clear();
            switch (Configuration.Instance.DataSourceType)
            {
                case DataSourceTypeEnum.SqlServer:
                    cache.Add(ConnectionName, "SqlConnection");
                    cache.Add(CommandName, "SqlCommand");
                    cache.Add(ParameterName, "SqlParameter");
                    cache.Add(ClientNameSpaceName, "System.Data.SqlClient");
                    cache.Add(DbTypeNameSpaceName, "System.Data");
                    cache.Add(NamePrefixName, "Sql");
                    cache.Add(TimestampName, "Timestamp");
                    cache.Add(AssemblyName, "System.Data");
                    break;
                case DataSourceTypeEnum.Oracle:
                    cache.Add(ConnectionName, "OracleConnection");
                    cache.Add(CommandName, "OracleCommand");
                    cache.Add(ParameterName, "OracleParameter");
                    cache.Add(ClientNameSpaceName, "Oracle.DataAccess.Client");
                    cache.Add(DbTypeNameSpaceName, "Oracle.DataAccess.Types");
                    cache.Add(NamePrefixName, "Oracle");
                    cache.Add(TimestampName, "TimeStamp");
                    cache.Add(AssemblyName, "Oracle.DataAccess");
                    break;
            }
        }
        public static string DbConnectionName
        {
            get
            {
                return cache[ConnectionName];
            }
        }

        public static string DbCommandName
        {
            get
            {
                return cache[CommandName];
            }
        }

        public static string DbParameterName
        {
            get
            {
                return cache[ParameterName];
            }
        }
        public static string ClientNamespace
        {
            get
            {
                return cache[ClientNameSpaceName];
            }
        }

        public static string DbTypeNamespace
        {
            get
            {
                return cache[DbTypeNameSpaceName];
            }
        }

        public static string NamePrefix
        {
            get
            {
                return cache[NamePrefixName];
            }
        }

        public static string Timestamp
        {
            get
            {
                return cache[TimestampName];
            }
        }

        public static string DbAssembly
        {
            get
            {
                return cache[AssemblyName];
            }
        }

        public static string GenRefCursor(DataSourceTypeEnum dst,string commandName,string paraName)
        {
            string result = "";
            if (dst == DataSourceTypeEnum.Oracle)
            {
                result = string.Format("{0}.Parameters.Add(ADOHelper.CreateRefCursorParameter(\"{1}\"));", commandName, paraName);
            }
            return result;

        }
    }
}
